package menu;

import graphics.ImageHandler;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;

import menu.OptionButton.ReactionType;

import core.Constants;
import core.GameOptions;
import core.MessageHandler;
import core.SingleButtonGame;
import core.gamemodes.GameMode;

public class MainMenu implements KeyListener{

	int mapId = 0;
	String[] mapList;
	Vector<OptionButton> options = new Vector<OptionButton>();
	
	public MainMenu(){
		MessageHandler.showMessage("If you're new, change the gamemode to Tutorial, and then continue using 'space'", "iconInfo.png");
		File f = new File(GameOptions.mapFolder);
		mapList = f.list();
		toggleMap();
		
		setupOptions();
	}
	
	private void setupOptions(){
		OptionButton o;
		Vector<String> choices;
		String optionName;
		ReactionType reaction;
		int startOption = 1;
		int x = 50;
		int y = 150;
		
		choices = new Vector<String>();
		reaction = ReactionType.MAP;
		optionName = "Map:";
		for(String s : mapList){
			if(s.endsWith(".txt"))
				choices.add(s);
		}
		startOption = 1;
		o = new OptionButton(optionName, reaction, choices, x, y, 900, startOption);
		options.add(o);
		y += 35;
		
		choices = new Vector<String>();
		reaction = ReactionType.MAXPLAYERS;
		optionName = "Players:";		
		choices.add("4");
		choices.add("9");
		choices.add("12");
		choices.add("16");
		startOption = 1;
		o = new OptionButton(optionName, reaction, choices, x, y, 500, startOption);
		options.add(o);
		y += 35;
		
		choices = new Vector<String>();
		reaction = ReactionType.GAMEMODE;
		optionName = "Gamemode:";		
		for(GameMode gm : GameOptions.getAvailableGameModes()){
			choices.add(gm.getName());				
		}	
		startOption = 1;
		o = new OptionButton(optionName, reaction, choices, x, y, 700, startOption);
		options.add(o);
		y += 35;
		
	}
	
	public void paint(Graphics g){
		
		g.setColor(Color.green);
		g.fillRect(0,0,Constants.screenSize.width,Constants.screenSize.height);
		ImageHandler.getInstance().drawImage(0, 0, Constants.screenSize.width,Constants.screenSize.height, "menuBg.jpg", g);
		
		g.setFont(Constants.defaultFont);
		g.setColor(Color.black);
		g.drawString("Press space to start the game", 50, 80);
		g.drawString("gamemoDe: "+GameOptions.selectedGameMode.getName(), 50, 100);
		g.drawString("starting Gold: "+GameOptions.startGold, 50, 120);
		g.drawString("Map: "+GameOptions.mapName, 50, 140);
		
		for(OptionButton o : options){
			o.paint(g);
		}
	
	}

	@Override
	public void keyPressed(KeyEvent e) {
		char c = e.getKeyChar();
		switch(c){
		case 'd': //gamemoDe
			toggleGamemode();
			break;
		case 'p': //max Players
			break;
		case 'g': // starting Gold
			toggleStartGold();
			break;
		case 'm': // Map
			toggleMap();
			break;
		case ' ': // start game
			SingleButtonGame.getInstance().initConfiguration();
			break;
		default:
			
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	private void toggleStartGold(){
		if(GameOptions.startGold >= 300)
			GameOptions.startGold = 50;
		else
			GameOptions.startGold = GameOptions.startGold + 50;
			
	}
	
	private void toggleMap(){
		do{
			mapId++;
			if(mapId >= mapList.length)
				mapId = 0;
			GameOptions.mapName = mapList[mapId];
		}while (!GameOptions.mapName.endsWith(".txt"));
		
	}
	
	private void toggleGamemode(){
		int i = GameOptions.getAvailableGameModes().indexOf(GameOptions.selectedGameMode);
		i++;
		if(i >= GameOptions.getAvailableGameModes().size())
			i=0;
		GameOptions.selectedGameMode=GameOptions.getAvailableGameModes().get(i);
		
		/*switch(GameOptions.gameMode){
		case FREE_FOR_ALL:
			GameOptions.gameMode = GameMode.TEAMS;
			break;
		case TEAMS:
			GameOptions.gameMode = GameMode.SURVIVAL;
			break;
		default:
			GameOptions.gameMode = GameMode.FREE_FOR_ALL;
		break;
		}*/
	}
	
}
